package com.xm.servlet.admin;

import com.xm.biz.UsersBiz;
import com.xm.biz.impl.UsersBizImpl;
import com.xm.po.Users;
import com.xm.util.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-08-27 10:39
 * @desc 导出Excel表格处理器
 **/
@WebServlet("/excel")
public class ExcelServlet extends HttpServlet {
    UsersBiz usersBiz = new UsersBizImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Users> users = usersBiz.userQueryAll();
        String [] headerName = {"用户id","账号","安全码","密码","性别","会员","用户名称","QQ","手机","邮箱","积分","头像","用户历史消费积分","账户状态",
                "用户等级","删除标志","创建时间","最后登陆ip","最后登陆时间","用户来源","openId","wxOpenId","账户余额","冻结金额","佣金","是否购买者","支付密码"};
        String[] headerKey = {"userId","loginName","loginSecret","loginPwd","userSex","userType","userName","userQQ",
                "userPhone","userEmail","userScore","userPhoto","userTotalScore","userStatus","userLevel","userFlag",
                "createTime","lastIP","lastTime","userFrom","openId","wxOpenId","userMoney","lockMoneyl",
                "distributMoney","isBuyer","payPwd"};
        HSSFWorkbook wb = ExcelUtils.createExcel(headerName, headerKey, "年数据管理", users);
        if (wb == null) {
            return;
        }
        resp.setContentType("application/vnd.ms-excel");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = sdf.format(date);
        String fileName = "学生信息管理" + str;
        resp.setHeader("Content-disposition",
                "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1") + ".xls");
        OutputStream ouputStream = resp.getOutputStream();
        ouputStream.flush();
        wb.write(ouputStream);
        ouputStream.close();
    }
}
